package com.itsu.vtalk.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsu.vtalk.dao.MessageDao;
import com.itsu.vtalk.entity.Message;
import com.itsu.vtalk.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author suben
 * @createtime 2020/6/20 16:16
 */
@Service
public class MessageServiceBase implements MessageService {
    @Resource
    private MessageDao messageDao;

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void saveUserMessage(Message message) throws Exception {
        messageDao.insert(message);
    }


    @Override
    public List<Message> getLast30DaysMessage(String fromUserId, String toUserId) throws Exception {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(fromUserId))
            queryWrapper.eq("from_user_id", fromUserId);
        if (StringUtils.isNotBlank(toUserId))
            queryWrapper.eq("to_user_id", toUserId);
        queryWrapper.gt("create_time", DateUtil.offsetDay(new Date(), -30));
        return messageDao.selectList(queryWrapper);
    }

    @Override
    public List<Message> getLast30DaysGroupMessage() throws Exception {
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_user_id", "all")
                .lt("create_time", DateUtil.offsetDay(new Date(), -30));
        return messageDao.selectList(queryWrapper);
    }
}
